package org.pisces.wechat.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.pisces.wechat.annotation.Anonymous;
import org.pisces.wechat.commons.WechatConstants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

	@RequestMapping("/auth")
	public String auth(HttpServletRequest request,String redirect) throws UnsupportedEncodingException{
		
		String resultUrl = URLEncoder.encode("http://wechat.songxuan.org"+redirect,"utf-8");
		StringBuilder sb = new StringBuilder();
		sb.append(WechatConstants.WECHAT_OPEN_URL).append("/connect/oauth2/authorize").append("?appid=").append("wx66e4e106a9a621f8").append("&redirect_uri=")
				.append(resultUrl).append("&response_type=code&scope=snsapi_userinfo");
		return "redirect:"+sb.toString();
	}
	
	@RequestMapping("/user-info")
	@ResponseBody
	public String userInfo(HttpServletRequest request,String name){
		String user = (String) request.getSession().getAttribute("CURRENT_LOGIN_USER");
		return "name = "+name +"--->user="+user;
	}
	
	@RequestMapping("/user")
	@ResponseBody
	@Anonymous
	public String user(HttpServletRequest request,String name){
		String user = (String) request.getSession().getAttribute("CURRENT_LOGIN_USER");
		if(user==null){
			return "匿名访问";
		}
		return "name = "+name +"--->user="+user;
	}
	
	
}
